package com.fancy.learncenter.ui.activity;

import android.os.Bundle;
import android.support.v4.app.Fragment;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.widget.ImageView;

import com.fancy.learncenter.R;
import com.fancy.learncenter.ui.activity.base.BaseActivity;
import com.fancy.learncenter.ui.adapter.FragPagerAdapter;
import com.fancy.learncenter.ui.fragment.CartoonLocalListenFragment;
import com.fancy.learncenter.ui.fragment.CartoonReleaseListenFragment;
import com.fancy.learncenter.utils.LogUtil;

import java.util.ArrayList;
import java.util.List;

import butterknife.Bind;
import butterknife.ButterKnife;
import butterknife.OnClick;

public class CartoonMyListenAndSpeakActivity extends BaseActivity {
    public static String FILE_PATH_KEY = "FILE_PATH_KEY";
    String fileResPath;

    @Bind(R.id.view_pager)
    ViewPager viewPager;
    @Bind(R.id.top_img)
    ImageView topImg;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setCartoonContentView(R.layout.activity_cartoon_my_listen_and_speak, Status.STATUS);
        ButterKnife.bind(this);

        fileResPath = getIntent().getStringExtra(FILE_PATH_KEY);
        List<Fragment> fragments = new ArrayList<>();
        fragments.add(new CartoonReleaseListenFragment());
        fragments.add(CartoonLocalListenFragment.newInstance(fileResPath));

        List titles = new ArrayList();
        titles.add("已发布的音频");
        titles.add("本地音频");
        FragPagerAdapter mFragmentAdapteradapter =
                new FragPagerAdapter(getSupportFragmentManager(), fragments, titles);
        //给ViewPager设置适配器
        viewPager.setAdapter(mFragmentAdapteradapter);

        viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
            @Override
            public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

            }

            @Override
            public void onPageSelected(int position) {
                if (position == 0) {
                    topImg.setImageResource(R.mipmap.cartoon_everyday_release);
                } else if (position == 1) {
                    topImg.setImageResource(R.mipmap.cartoon_everyday_local);
                }
            }

            @Override
            public void onPageScrollStateChanged(int state) {

            }
        });

    }

    @OnClick({R.id.release, R.id.local})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.release:
                if (viewPager.getCurrentItem() == 0) {
                    return;
                }
                topImg.setImageResource(R.mipmap.cartoon_everyday_release);
                viewPager.setCurrentItem(0, true);

                break;
            case R.id.local:
                if (viewPager.getCurrentItem() == 1) {
                    return;
                }
                topImg.setImageResource(R.mipmap.cartoon_everyday_local);
                viewPager.setCurrentItem(1, true);
                break;
        }
    }
}
